package service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import entity.User;
import mapper.UserMapper;

@Service
public class UserService {
    //此处变量是接口类型，不加@Autowired
    private UserMapper userMapper;

    @Autowired
    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public boolean login(User user) {
        System.out.println("user:"+user);
        User u = userMapper.findUser(user);
        System.out.println("u:"+u);
        if (u != null)
            return true;
        else {
            return false;
        }
    }

    @Transactional//增删改操作要添加@Transactional事务注解,这样程序无需添加提交、回滚等事务操作(将由spring接管)
    public boolean register(User user) {
        int cnt = userMapper.addUser(user);
        if (cnt != 0)
            return true;
        else {
            return false;
        }
    }

    public boolean findUser(String username) {
        User u = userMapper.findUserByUserName(username);
        if (u != null)
            return true;
        else {
            return false;
        }
    }
}